/* eslint-disable */ 
import axios from 'axios'
import Vue from 'vue'
import  sinochem  from '@/services/sinochem.js'
// import  {appkey } from '../../public/appconfig'; 

axios.defaults.baseURL = '/api/CrmData/'
axios.defaults.headers.post['Content-Type'] = 'application/json'
var isRequest = 0
var isreject = true
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    // tryHideFullScreenLoading()
    return response
  }, function (error) {
    if (error.response.status === 401 && isreject) {
       //alert('暂无登陆')
      isreject = false
    }
    // tryHideFullScreenLoading()
    // 对响应错误做点什么
    return Promise.reject(error)
})


class dataservice {

    async dataRouter(routerName, requestParam) {
        var list = await axios.post('/' + routerName, requestParam)
        return list
    }
    /**
     * 登录
     */
    async login(username) {
        var response = await axios.post('/appLogin', {
            username: username
        })
        return response
    }
    //获取地址email解密
    async getLoginEmail(routerName){   
        let email= routerName.query.email  
        if (window.location.href.indexOf('localhost') >0) {
            return 'lipushi'
        } 
        if(!email){
            return ''
        }  
        
        var str = email; 
        var key1 = key; 
        var key2 = "ecology"; 
        var key3 = "sinochem"; 
        var enResult = sinochem.strDec(str, key1, key2, key3);   
        return enResult
        //this.decryptText('6D6B16837DF9982C7FE91C89E7F0C0B7')
    } 
    /**
   * 以管理员通过key查询系统配置表
   * @param {配置参数Key} configKey
   */
    getSystemConfig (configKey) {
        return axios.post('/Get', {
        uri: "jm_systemconfigurations?$select=jm_parameter,&$filter=jm_code eq '" + configKey + "'",
        formatted: false
        })
    }

    //获取区域列表
    async getAreaList() { 
        var userid = Vue.cookies.get('UserId') 
        return await axios.post('/Get', {
            uri: 'jm_areas?$select=jm_name,jm_coordinate&$orderby=jm_name',
            formatted: false,
            userid: userid
        })
    } 
    //获取城市数量
    async getCityCount() {   
        var list = await axios.post('/Execute', {
            uri: 'jm_get_totalpage',
            entData: {
            entName: 'jm_countcity',
            filters: '',
            columns: '1',
            keyValue: '',
            prefilters: ' 1=1 and statecode=0 ',
            orderby: ' createdon',
            pagesize: 1,
            pagenum: 1
            },
            userid: ''
        })
        return list 
    } 
    //获取运营项目数量
    async getOperationprojectCount() {   
        var list = await axios.post('/Execute', {
            uri: 'jm_get_totalpage',//jm_get_totalpage
            entData: {
            entName: 'jm_countproject',
            filters: '',
            columns: ' createdon ',
            keyValue: '',
            prefilters: ' 1=1 and statecode=0 ',
            orderby: ' createdon desc',
            pagesize: 1,
            pagenum: 1
            },
            userid: ''
        })
        return list 
    }    
    //获取年度楼栋净签金额
    async getAnnualnetamount(){ 
        return await axios.post('/Get', {
            uri: 'jm_buildingdatasummaries?$select=jm_indexvalue,jm_areaid&$orderby=jm_indexvalue',
            formatted: true,
            userid:  Vue.cookies.get('UserId') 
        })
    }
    formatDate(date, fmt) { 
        if (/(y+)/.test(fmt)) {
       fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
     }
     let o = {
       'M+': date.getMonth() + 1,
       'd+': date.getDate(),
       'h+': date.getHours(),
       'm+': date.getMinutes(),
       's+': date.getSeconds()
     };
     for (let k in o) {
       if (new RegExp(`(${k})`).test(fmt)) {
         let str = o[k] + '';
         fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
       }
     }
     return fmt;
   }
    padLeftZero(str) {
     return ('00' + str).substr(str.length);
   }
 /**
   * 批量更新创建记录
   */
  upsertRecords (params, userid) {
    if (isRequest) {
      return new Promise(() => {})
    }
    isRequest = 1
    return this.dataRouter('execute', {
      uri: 'jm_do_upserts',
      entData: {
        params: params
      },
      userid: userid
    }).then((state) => {
      isRequest = 0
      return state
    })
  }
   /** 创建用户访问记录
    *  @param {*}model  实体所需赋值字段组
    * @param {*} userid
    * */
  async createdUserRecord (model, userid) {
    let params = []
    let ent = {
      name: 'jm_userlogin',
      id: '',
      data: model
    }
    params.push(ent)
    let httpResults =await this.upsertRecords(JSON.stringify(params), userid)
    if (httpResults.status === 200 && httpResults.data.res === 'success') {
      return true
    } else {
      return false
    }
  }

}
export default new dataservice()